<?php

/**
 * This is the model class for table "announcement_assign".
 *
 * The followings are the available columns in table 'announcement_assign':
 * @property integer $member_id
 * @property integer $announcement_id
 * @property integer $purpose_join
 * @property integer $event_joined
 */
class AnnouncementAssignBase extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return AnnouncementAssignBase the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'announcement_assign';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('member_id, announcement_id, purpose_join, event_joined', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('member_id, announcement_id, purpose_join, event_joined', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'member' => array(self::BELONGS_TO, 'Member', 'member_id'),
			'announcement' => array(self::BELONGS_TO, 'Announcement', 'announcement_id', 'joinType' => 'INNER JOIN'),
		);
	}

	public function countAsignByMember($member_id, $type = 'asign', $start = '', $end = '') {
		// Get box
		$criteria = new CDbCriteria();
		$criteria->with 		= array('announcement');
		$criteria->condition 	= "event_fg = 1 and t.member_id = :member_id";
		$params[':member_id'] 	= $member_id;

		if ($start != '') {
			$criteria->addCondition('DATE_FORMAT(announcement.created, "%Y%m") >= :start');
			$start	= mb_substr($start, 3, 4) . mb_substr($start, 0, 2);
			$params[':start'] 	= $start;
		}

		if ($end != '') {
			$criteria->addCondition('DATE_FORMAT(announcement.created, "%Y%m") <= :end');
			$end	= mb_substr($end, 3, 4) . mb_substr($end, 0, 2);
			$params[':end'] 	= $end;
		}

		if ($type == 'joined') {
			$criteria->addCondition('t.event_joined = :event_joined');
			$params[':event_joined'] 	= 1;
		}
		
		$criteria->params = $params;
		return $this->count($criteria);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('member_id',$this->member_id);
		$criteria->compare('announcement_id',$this->announcement_id);
		$criteria->compare('purpose_join',$this->purpose_join);
		$criteria->compare('event_joined',$this->event_joined);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}